package com.kaishengit.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.kaishengit.pojo.Goal;
import com.kaishengit.pojo.Task;
import com.kaishengit.pojo.User;
import com.kaishengit.service.GoalService;
import com.kaishengit.service.TaskService;
import com.kaishengit.service.UserService;

@Controller
@Results({
	@Result(name = "savetaskSuccess", location = "task.action", type = "redirectAction", params = {"id", "${id}" }) ,
	@Result(name = "editSuccess", location = "task.action", type = "redirectAction", params = {"id", "${id}" }) 
})
public class TaskAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private TaskService taskService;
	private GoalService goalService;
	private UserService userService;

	private List<Task> taskList;
	private List<User> userList;

	private User user;
	private Goal goal;
	private Task task;
	private String id;
	private String username;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	@Action("task")
	public String finAll() {

		goal = goalService.findById(id);
		taskList = taskService.finAllTask(goal);
		return SUCCESS;
	}

	@Action("toedittask")
	public String toedittask() {
		userList=userService.findAll();
		task=taskService.findById(id);
		return SUCCESS;
	}

	@Action("edittask")
	public String edittask() {
		user = userService.findByUsername(username);
		taskService.update(task, id,user);
		id=taskService.findById(id).getGoal().getId();
		return "editSuccess";
	}

	@Action("toaddtask")
	public String toAdd() {

		userList = userService.findAll();
		return SUCCESS;
	}

	@Action("addtask")
	public String savetask() {
		goal = goalService.findById(id);
		user = userService.findByUsername(username);
		taskService.save(task, user, goal);
		id = goal.getId();
		return "savetaskSuccess";
	}

	@Autowired
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	@Autowired
	public void setGoalService(GoalService goalService) {
		this.goalService = goalService;
	}

	public List<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Goal getGoal() {
		return goal;
	}

	public void setGoal(Goal goal) {
		this.goal = goal;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
